﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.Utility;
using VAdvantage.DataBase;
using VAdvantage.Logging;
using System.Data;
using VAdvantage.Classes;
namespace VAdvantage.Model
{
    public class MModuleInfo:VAdvantage.Model.X_AD_ModuleInfo
    {
    public MModuleInfo(Ctx ctx, int AD_ModuleInfo_ID, string trx)
            : base(ctx, AD_ModuleInfo_ID, trx)
    {
 
    }

        ///// <summary>
        ///// Before Save
        ///// </summary>
        ///// <param name="newRecord"></param>
        ///// <returns></returns>
    protected override bool BeforeSave(bool newRecord)
    {

        string Sql = "Select C_BPartner_Id from AD_ModuleRelatedInfo where AD_ModuleInfo_ID=" + GetAD_ModuleInfo_ID();

        int c_BPartner_ID = 0;
        int prefixCount = 0;
        var obj = DB.ExecuteScalar(Sql);
        if (obj != DBNull.Value)
        {
            c_BPartner_ID = Convert.ToInt32(obj);

        }

        string prefix = "";

        if (GetPrefix() != null)
        {
            prefix = GetPrefix().ToUpper();
        }
        if (prefix != "")
        {
            Sql = "Select count(prefix) from AD_ModuleInfo where prefix='" + prefix + "'" + " and ad_moduleinfo_id!=" + GetAD_ModuleInfo_ID(); ;
            //obj = DB.ExecuteScalar(Sql);
            //if (obj != DBNull.Value)
            // {
            prefixCount = Convert.ToInt32(DB.ExecuteScalar(Sql));
            //}


        }

        if (prefixCount > 0 && c_BPartner_ID == 0)
        {
            log.SaveError("PrefixNotAvailable", "", false);
            //ShowMessage.Info("PrefixNotAvailable", true, "", null);
            return false;
        }

        int isPrefixBind = -1;

        //check prefix is bound to same BPartner
        if (c_BPartner_ID > 0 && prefix != "" && prefixCount > 0)
        {
            Sql = "Select count(prefix) from ad_moduleinfo MI inner join " +
                "AD_ModuleRelatedInfo MRI on MI.ad_moduleinfo_ID= MRI.ad_moduleinfo_ID where MRI.C_BPartner_ID=" + c_BPartner_ID +
                " AND MI.Prefix='" + prefix + "'";

            // obj = DB.ExecuteScalar(Sql);

            // if (obj != DBNull.Value)
            // {
            isPrefixBind = Convert.ToInt32(DB.ExecuteScalar(Sql));
            // }


        }

        if (isPrefixBind == 0)
        {
            log.SaveError("PrefixNotAvailable", "", false);
            //ShowMessage.Info("PrefixNotAvailable", true, "", null);
            return false;
        }


        //Check Assembly Name
        string assemblyname = "";
        if (GetAssemblyName() != null)
        {
            assemblyname = GetAssemblyName().ToUpper();
        }

        int asmCount = 0;
        if (assemblyname != "")
        {
            Sql = "Select count(assemblyname) from AD_Moduleinfo where UPPER(assemblyname)='" + assemblyname + "'" + " and ad_moduleinfo_id!=" + GetAD_ModuleInfo_ID();
            asmCount = Convert.ToInt32(DB.ExecuteScalar(Sql));
        }

        if (asmCount > 0 && c_BPartner_ID == 0)
        {
            log.SaveError("AssemblyNameNotAvailable", "", false);
            //ShowMessage.Info("AssemblyNameNotAvailable", true, "", null);
            return false;
        }

        int asmbind = -1;



        if (asmCount > 0 && c_BPartner_ID > 0 && assemblyname != "")
        {
            Sql = "Select count(assemblyname) from ad_moduleinfo MI inner join " +
                   " AD_ModuleRelatedInfo MRI on MI.ad_moduleinfo_ID= MRI.ad_moduleinfo_ID " +
                   " WHERE MI.Name!='" + GetName() + "'" +
                   " AND UPPER(MI.assemblyname)='" + assemblyname + "'";
            asmbind = Convert.ToInt32(DB.ExecuteScalar(Sql));
        }

        if (asmbind > 0)
        {
            log.SaveError("AssemblyNameNotAvailable", "", false);
            //ShowMessage.Info("AssemblyNameNotAvailable", true, "", null);
            return false;
        }


        return base.BeforeSave(newRecord);
    }

    }
}
